home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / testdisk / src / edit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-01-02  |  6.7 KB  |  282 lines

  1. /*
  2.  
  3.     File: edit.c
  4.  
  5.     Copyright (C) 1998-2004 Christophe GRENIER <grenier@cgsecurity.org>
  6.   
  7.     This software is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.   
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.   
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.  */
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <unistd.h>    /* geteuid */
  28. #include "types.h"
  29. #include "common.h"
  30. #include "lang.h"
  31. #include "intrface.h"
  32. #include "fnctdsk.h"
  33.  
  34. static void interface_editor_position(const t_param_disk *disk_car, unsigned long int *lba);
  35. static int dump_editor(const unsigned char *nom_dump,const unsigned int lng, const int menu_pos);
  36.  
  37. void interface_editor(t_param_disk *disk_car)
  38. {
  39.   int done = FALSE;
  40.   dword hd_offset=0;
  41.   ecrit_rapport("%s",disk_car->description(disk_car));
  42.   while (done==FALSE)
  43.   {
  44.     static struct MenuItem menuEditor[]=
  45.     {
  46.       { 'C', "Change position", "" },
  47.       { 'D', "Dump", "Dump sector" },
  48.       { 'Q', "Quit",""},
  49.       { 0, NULL, NULL }
  50.     };
  51.     switch (toupper( wmenuSelect(stdscr,INTER_DUMP_Y, INTER_DUMP_X, menuEditor, 8, "CDQ", MENU_HORIZ | MENU_BUTTON | MENU_ACCEPT_OTHERS, 0)))
  52.     {
  53.       case 'C':
  54.     interface_editor_position(disk_car,&hd_offset);
  55.     break;
  56.       case 'D':
  57.     {
  58.       unsigned char buffer[SECTOR_SIZE];
  59.       int menu_pos=MENU_DOWN;
  60.       while(done==0)
  61.       {
  62.         wmove(stdscr,4,0);
  63.         wclrtoeol(stdscr);
  64.         wdoprintf(stdscr,"%ld ", hd_offset);
  65.         aff_LBA2CHS(disk_car,hd_offset);
  66.         if(disk_car->read(disk_car,1, buffer, hd_offset))
  67.           wdoprintf(stdscr,msg_PART_RD_ERR);
  68.         else
  69.         {
  70.           menu_pos=dump_editor(buffer,SECTOR_SIZE,menu_pos);
  71.           switch(menu_pos)
  72.           {
  73.         case MENU_UP:
  74.           if(hd_offset>0)
  75.             hd_offset--;
  76.           else
  77.             menu_pos=MENU_DOWN;
  78.           break;
  79.         case MENU_DOWN:
  80.           if(hd_offset<disk_car->size-1)
  81.             hd_offset++;
  82.           else
  83.             menu_pos=MENU_UP;
  84.           break;
  85.         default:
  86.           done = TRUE;
  87.           break;
  88.           }
  89.         }
  90.       }
  91.       done = FALSE;
  92.     }
  93.     break;
  94.       case key_ESC:
  95.       case 'Q':
  96.     done = TRUE;
  97.     break;
  98.     }
  99.   }
  100. }
  101.  
  102. static void interface_editor_position(const t_param_disk *disk_car,unsigned long int *lba)
  103. {
  104.   t_CHS position;
  105.   int done = FALSE;
  106.   char def[LINE_LENGTH];
  107.   char response[LINE_LENGTH];
  108.   unsigned int tmp_val;
  109.   int command;
  110.   position.cylinder=LBA2cylinder(disk_car,*lba);
  111.   position.head=LBA2head(disk_car,*lba);
  112.   position.sector=LBA2sector(disk_car,*lba);
  113.   while (done==FALSE) {
  114.     static struct MenuItem menuGeometry[]=
  115.     {
  116.       { 'c', "Cylinders", "Change cylinder" },
  117.       { 'h', "Heads", "Change head" },
  118.       { 's', "Sectors", "Change sector" },
  119.       { 'd', "Done", "Done with changing" },
  120.       { 0, NULL, NULL }
  121.     };
  122.     wmove(stdscr,COMMAND_LINE_Y, COMMAND_LINE_X);
  123.     wclrtoeol(stdscr);
  124.     wrefresh(stdscr);
  125.     command=wmenuSimple(stdscr,menuGeometry, 3);
  126.     command=toupper(command);
  127.     switch (command) {
  128.       case 'C':
  129.         sprintf(def, "%u", position.cylinder);
  130.         mvwaddstr(stdscr,COMMAND_LINE_Y, COMMAND_LINE_X, "Enter the number of cylinders: ");
  131.         if (get_string(response, LINE_LENGTH, def) > 0) {
  132.           tmp_val = atoi(response);
  133.           if (tmp_val <= disk_car->CHS.cylinder) {
  134.             position.cylinder = tmp_val;
  135.           } else
  136.             wdoprintf(stdscr,"Illegal cylinders value");
  137.         }
  138.         break;
  139.       case 'H':
  140.         sprintf(def, "%u", position.head);
  141.         mvwaddstr(stdscr,COMMAND_LINE_Y, COMMAND_LINE_X, "Enter the number of heads: ");
  142.         if (get_string(response, LINE_LENGTH, def) > 0) {
  143.           tmp_val = atoi(response);
  144.           if (tmp_val <= disk_car->CHS.head) {
  145.             position.head = tmp_val;
  146.           } else
  147.             wdoprintf(stdscr,"Illegal heads value");
  148.         }
  149.         break;
  150.       case 'S':
  151.         sprintf(def, "%u", position.sector);
  152.         mvwaddstr(stdscr,COMMAND_LINE_Y, COMMAND_LINE_X, "Enter the number of sectors per track: ");
  153.         if (get_string(response, LINE_LENGTH, def) > 0) {
  154.           tmp_val = atoi(response);
  155.           if (tmp_val > 0 && tmp_val <= disk_car->CHS.sector ) {
  156.             position.sector = tmp_val;
  157.           } else
  158.             wdoprintf(stdscr,"Illegal sectors value");
  159.         }
  160.         break;
  161.       case key_ESC:
  162.       case 'D':
  163.         done = TRUE;
  164.         break;
  165.     }
  166.   }
  167.   *lba=CHS2LBA(disk_car,&position);
  168. }
  169.  
  170. static int dump_editor(const unsigned char *nom_dump,const unsigned int lng, const int menu_pos)
  171. {
  172.   unsigned int i,j;
  173.   unsigned int pos;
  174.   unsigned char car;
  175.   int done=0;
  176.   unsigned int menu;
  177.   struct MenuItem menuDump[]=
  178.   {
  179.     { 'P', "Previous",""},
  180.     { 'N', "Next","" },
  181.     { 'Q',"Quit","Quit dump section"},
  182.     { 0, NULL, NULL }
  183.   };
  184.   /* write dump to log file*/
  185.   for (i=0; (i<lng/0x10); i++)
  186.   {
  187.     ecrit_rapport("%04X ",i*0x10);
  188.     for(j=0; j< 0x10;j++)
  189.     {
  190.       car=*(nom_dump+i*0x10+j);
  191.       ecrit_rapport("%02x", car);
  192.       if(j%4==(4-1))
  193.         ecrit_rapport(" ");
  194.     }
  195.     ecrit_rapport("  ");
  196.     for(j=0; j< 0x10;j++)
  197.     {
  198.       car=*(nom_dump+i*0x10+j);
  199.       if ((car<32)||(car >= 127))
  200.         ecrit_rapport(".");
  201.       else
  202.         ecrit_rapport("%c",  car);
  203.     }
  204.     ecrit_rapport("\n");
  205.   }
  206.   /* ncurses interface */
  207.   pos=(menu_pos==MENU_DOWN?0:lng/0x10-DUMP_MAX_LINES);
  208.   menu=(menu_pos==MENU_DOWN?1:0);
  209.   mvwaddstr(stdscr,DUMP_Y,DUMP_X,msg_DUMP_HEXA);
  210.   do
  211.   {
  212.     for (i=pos; (i<lng/0x10)&&((i-pos)<DUMP_MAX_LINES); i++)
  213.     {
  214.       wmove(stdscr,DUMP_Y+i-pos,DUMP_X);
  215.       wclrtoeol(stdscr);
  216.       wdoprintf(stdscr,"%04X ",i*0x10);
  217.       for(j=0; j< 0x10;j++)
  218.       {
  219.         car=*(nom_dump+i*0x10+j);
  220.         wdoprintf(stdscr,"%02x", car);
  221.         if(j%4==(4-1))
  222.           wdoprintf(stdscr," ");
  223.       }
  224.       wdoprintf(stdscr,"  ");
  225.       for(j=0; j< 0x10;j++)
  226.       {
  227.         car=*(nom_dump+i*0x10+j);
  228.         if ((car<32)||(car >= 127))
  229.           wdoprintf(stdscr,".");
  230.         else
  231.           wdoprintf(stdscr,"%c",  car);
  232.       }
  233.     }
  234.     switch (wmenuSelect(stdscr,INTER_DUMP_Y, INTER_DUMP_X, menuDump, 8, "PNQ", MENU_HORIZ | MENU_BUTTON | MENU_ACCEPT_OTHERS, menu))
  235.     {
  236.       case 'p':
  237.       case 'P':
  238.       case MENU_UP:
  239.         menu=0;
  240.         if(pos>0)
  241.           pos--;
  242.         else
  243.           done=MENU_UP;
  244.         break;
  245.       case 'n':
  246.       case 'N':
  247.       case MENU_DOWN:
  248.         menu=1;
  249.         if(pos<lng/0x10-DUMP_MAX_LINES)
  250.           pos++;
  251.         else
  252.           done = MENU_DOWN;
  253.         break;
  254.       case KEY_PPAGE:
  255.         menu=0;
  256.         if(pos==0)
  257.           done=MENU_UP;
  258.         if(pos>DUMP_MAX_LINES-1)
  259.           pos-=DUMP_MAX_LINES-1;
  260.         else
  261.           pos=0;
  262.         break;
  263.       case KEY_NPAGE:
  264.         menu=1;
  265.         if(pos==lng/0x10-DUMP_MAX_LINES)
  266.           done=MENU_DOWN;
  267.         if(pos<lng/0x10-DUMP_MAX_LINES-(DUMP_MAX_LINES-1))
  268.           pos+=DUMP_MAX_LINES-1;
  269.         else
  270.           pos=lng/0x10-DUMP_MAX_LINES;
  271.         break;
  272.       case key_ESC:
  273.       case 'q':
  274.       case 'Q':
  275.         done = 'Q';
  276.         break;
  277.     }
  278.   } while(done==0);
  279.   return done;
  280. }
  281.  
  282.